package handingIn;

import com.icesoft.faces.context.FileResource;
import groups.ExerciseGroup;
import handingIn.HandingIn;
import handingIn.HandingInData;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import javax.faces.bean.CustomScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import lecture.Lecture;
import lecture.LectureManagedBean;
import task.ExerciseSheet;
import user.User;
import user.UserAdminManagedBean;

/**
 *
 * @author Thorsten Ludwig
 */
@ManagedBean(name="browseHandingIn")
@CustomScoped(value="#{window}")
public class BrowseHandingInManagedBean {
    
    private ExerciseSheet sheet;
    
    private HandingIn handingIn;
    
    private ExerciseGroup exerciseGroup;
    
    private User user;
    
    private List<BrowseContainer> files;
    
    public BrowseHandingInManagedBean() {
        this.files = new ArrayList<BrowseContainer>();
    }
    
    public FileResource getResource(File file) {
        if (file == null) {
            return null;
        } else {
            return new FileResource(file);
        }
    }
    public void doAction() {
        System.out.println("Starte filtern");
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        Collection<HandingInData> datas;
        files = new ArrayList<BrowseContainer>();
        
        List<ExerciseSheet> sheets = new ArrayList<ExerciseSheet>();
        if (this.sheet == null) {
            sheets.addAll(lecture.getExerciseSheets());
        } else {
            sheets.add(this.sheet);
        }
        
        List<HandingIn> handingIns = new ArrayList<HandingIn>();
        if (this.handingIn == null) {
            if (this.sheet == null) {
                for (ExerciseSheet es: lecture.getExerciseSheets()) {
                    handingIns.addAll(es.getHandingIns());
                }
            } else {
                handingIns.addAll(this.sheet.getHandingIns());
            }
        } else {
            handingIns.add(this.handingIn);
        }
        
        
        List<String> users = new ArrayList<String>();
        if (this.user == null) {
            if (this.exerciseGroup == null) {
                for (User u: lecture.getStudents()) {
                    users.add(u.getUsername());
                }
            } else {
                users.addAll(this.exerciseGroup.getStudents());
            }
        } else {
            users.add(this.user.getUsername());
        }
        
        for (ExerciseSheet es: lecture.getHandingIns().keySet()) {
            if (sheets.contains(es)) {
                Map<String, Map<HandingIn, File>> map = lecture.getHandingIns().get(es).getField();
                for (String username:map.keySet()) {
                    if (users.contains(username)) {
                        Map<HandingIn, File> handingInMap = map.get(username);
                        for (HandingIn h: handingInMap.keySet()) {
                            if (handingIns.contains(h)) {
                                files.add(new BrowseContainer(es.getName(), 
                                        h.getName(), 
                                        this.getUserExerciseGroup(username, lecture), 
                                        username,
                                        this.getResource(handingInMap.get(h))));
                            }
                        }
                    }
                }
            }
        }
        
        
    }
    
    public List<SelectItem> getSheets() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Kein Übungsblatt"));
        for (ExerciseSheet es: lecture.getExerciseSheets()) {
            list.add(new SelectItem(es, es.getName()));
        }
        return list;
    }
    
    public List<SelectItem> getHandingIns() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Keine Abgabe"));
        if (this.sheet == null) {
            for (ExerciseSheet es:lecture.getExerciseSheets()) {
                for (HandingIn h: es.getHandingIns()) {
                    list.add(new SelectItem(h,h.getName()));
                }
            }
        } else {
            for (HandingIn h: this.sheet.getHandingIns()) {
                list.add(new SelectItem(h,h.getName()));
            }
        }
        return list;
        
    }
    
    public List<SelectItem> getExerciseGroups() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Keine Übungsgruppe"));
        for (ExerciseGroup eg: lecture.getInfo().getExerciseGroups()) {
            list.add(new SelectItem(eg,eg.getName()));
        }
        return list;
        
    }
    
    public List<SelectItem> getUsers() {
        FacesContext c = FacesContext.getCurrentInstance();
        LectureManagedBean lb = c.getApplication().evaluateExpressionGet(c, "#{lectureBean}", LectureManagedBean.class);
        Lecture lecture = lb.getLecture();
        UserAdminManagedBean ua = c.getApplication().evaluateExpressionGet(c, "#{userAdminBean}", UserAdminManagedBean.class);
        List<SelectItem> list = new ArrayList<SelectItem>();
        list.add(new SelectItem(null, "Kein User"));
        if (this.exerciseGroup == null) {
            for (User u: lecture.getStudents()) {
                list.add(new SelectItem(u, u.getUsername()));
            }
        } else {
            for (String s:this.exerciseGroup.getStudents()) {
                User u = ua.getUser(s);
                list.add(new SelectItem(u,u.getUsername()));
            }
        }
        return list;
    }
    
    public ExerciseGroup getExerciseGroup() {
        return exerciseGroup;
    }

    public void setExerciseGroup(ExerciseGroup exerciseGroup) {
        System.out.println("Übungsgruppe gesetzt");
        this.exerciseGroup = exerciseGroup;
    }

    public HandingIn getHandingIn() {
        return handingIn;
    }

    public void setHandingIn(HandingIn handingIn) {
        System.out.println("Handing In gesetzt");
        this.handingIn = handingIn;
    }

    public ExerciseSheet getSheet() {
        return sheet;
    }

    public void setSheet(ExerciseSheet sheet) {
        System.out.println("Sheet gesetzt");
        this.sheet = sheet;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        System.out.println("User gesetzt");
        this.user = user;
    }

    private String getUserExerciseGroup(String username, Lecture lecture) {
        for (ExerciseGroup eg:lecture.getInfo().getExerciseGroups()) {
            if (eg.getStudents().contains(username))
                return eg.getName();
        }
        return "Keine Übungsgruppe";
    }

    public List<BrowseContainer> getFiles() {
        return files;
    }
}
